package utils;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletContext;
import javax.servlet.jsp.JspWriter;

import com.mysql.jdbc.PreparedStatement;

public class Printer {
		
	public static String dayString(int day) {
		switch(day) {
		case 1:
			return "Sunday";
		case 2:
			return "Monday";
		case 3: 
			return "Tuesday";
		case 4:
			return "Wednesday";
		case 5:
			return "Thursday";
		case 6:
			return "Friday";
		case 7:
			return "Saturday";
		default:
			return "BAD DAY";
		}
		
	}
	
	public static String hourString(int hour) {
		if (hour >= 0 && hour < 10) {
			return "0" + hour + ":00";
		}
		if (hour < 23) {
			return hour + ":00";
		}
		return "BAD HOUR";
				
	}
	
	
	
	static public void printCourses(JspWriter out, ResultSet res, String action, String hrefUrl, String submitValue, boolean printCourseAvilability, boolean printCouseIsActive, String username) 
		throws IOException, SQLException{
		out.println(
				"<form method=\"post\" action=\"" + action +"\">"  	+
				"<table border=\"10px\">"		 					+
				"<tr>"							 					+
					"<th>Select</th>"			 					+
					"<th>Name</th>"				 					+
					"<th>Credit Points</th>"	 					+
					"<th>Group Number</th>"		 					+
					(printCourseAvilability ? "<th>Available Places</th>" : "")	 	+
					(printCouseIsActive ? "<th>Comment</th>" : "")	 	+
					"</tr>");	
		
		
		int numCol = res.getMetaData().getColumnCount();
		while (res.next()) {
			out.println("<tr> <td> <input type = \"checkbox\" name=\"check" 
					+ "\" value =\"" + res.getString(1) + "\"" 
					+ ((printCourseAvilability && res.getInt(4) <= 0)  ? " disabled=\"disabled\"" : "") 
					+ " /> </td>");
			
			for (int i = 1; i <= numCol; i++) {
				out.println("<td>");
				if (i == 1) { // name col
					out.println("<a href=\"" + hrefUrl + "?course_name=" + res.getString(i) + "\">");
				}

				if (printCourseAvilability && i == 4 && res.getInt(i) <= 0){
						out.println("Course is Full");
				}
				else if (printCouseIsActive && i == 4){
					if (!res.getBoolean(i)) { // Course inactive
						out.println("Course is inactive, add at least one session");
					} 
				}
				else{
					out.println(res.getString(i));
				}
				
				if (i == 1) { // name col
					out.println("</a>");
				}
				out.println("</td>");
			}
			out.println("</tr>");	
		}
		out.println("</table>");
		out.println("<p><input type=\"submit\" value=\"" + submitValue + "\" /></p>");
		out.println("<p><input type=\"hidden\" name=\"username\" value=\"" + username +"\" /></p>");
		out.println("</form>");

	}
	
	static public void printCoursesAdmin(JspWriter out, ResultSet res, String action, String hrefUrl, String submitValue) 
	throws IOException, SQLException{
	out.println(
			"<form method=\"post\" action=\"" + action +"\">"  	+
			"<table border=\"10px\">"		 					+
			"<tr>"							 					+
				"<th>Select</th>"			 					+
				"<th>Name</th>"				 					+
				"<th>Credit Points</th>"	 					+
				"<th>Group Number</th>"		 					+
				"<th>Capacity</th>"              	 	        +
				"<th>Registered</th>"                    	 	+
				"<th>Comment</th>"                      	 	+
				"</tr>");	
	
	
	int numCol = res.getMetaData().getColumnCount();
	while (res.next()) {
		out.println("<tr> <td> <input type = \"checkbox\" name=\"check" 
				+ "\" value =\"" + res.getString(1) + "\""  
				+ " /> </td>");
		
		for (int i = 1; i <= numCol; i++) {
			out.println("<td>");
			if (i == 1) { // name col
				out.println("<a href=\"" + hrefUrl + "?course_name=" + res.getString(i) + "\">");
			}

			if (i == 6){
				if (!res.getBoolean(i)) { // Course inactive
					out.println("Course is inactive, click on the course name to activate");
				} 
			}
			else{
				out.println(res.getString(i));
			}
			
			if (i == 1) { // name col
				out.println("</a>");
			}
			out.println("</td>");
		}
		out.println("</tr>");	
	}
	out.println("</table>");
	out.println("<p><input type=\"submit\" value=\"" + submitValue + "\" /></p>");
	out.println("</form>");

}
	
	static public boolean printCourseSessions(ServletContext context, String action, JspWriter out, String courseName) 
	throws IOException, SQLException, ClassNotFoundException{
		
		DBConnector dbConn = null;
		try {
			dbConn = new DBConnector();
		

			PreparedStatement stmnt = (PreparedStatement) dbConn.getConnection().prepareStatement(
					"SELECT is_active " +
					"FROM courses WHERE course_name = ?");
			
			
			stmnt.setString(1, courseName);
			ResultSet res = stmnt.executeQuery();
			
			res.next();
			boolean isActive = res.getBoolean(1);
			
			stmnt = (PreparedStatement) dbConn.getConnection().prepareStatement(
						"SELECT session_day, session_start_time, session_end_time " +
						"FROM course_sessions WHERE course_name = ? ORDER BY session_day");
			
			stmnt.setString(1, courseName);
			res = stmnt.executeQuery();
			boolean isEmpty;
			isEmpty = !res.next(); //will be true if reultset is empty
			res.beforeFirst();
			if (isEmpty){
				out.println("<h3> No sessions for this course</h3>");
				return isActive;
			}
			
			if (isActive) {
				out.println("<h3>Course is active. You cannot edit sessions</h3>");
			}
			
			out.println(
					((action == null || isActive) ? "" : "<form method=\"post\" action=\"" + action +"\"><input type=\"hidden\" name=\"course_name\" value=\"" + courseName +  "\" />")  +
					"<table border=\"10px\">"		     +
					"<tr>"							     +
					(isActive ? "" : "<th>Select</th>")	 +
					"<th>Day</th>"			 		     +
					"<th>Start Time</th>"			     +
					"<th>End Time</th>"				     +
					"</tr>");	
			
			int numCol = res.getMetaData().getColumnCount();
			while (res.next()) {
				out.println("<tr>");
				if (!isActive) {
					out.println("<td> <input type = \"checkbox\" name=\"check" 
						+ "\" value =\"" + res.getString(1) + "," + res.getString(2) + ","  + res.getString(3) +  "\" /> </td>");
				}
				
				for (int i = 1; i <= numCol; i++) {
					out.println("<td>");
					if (i == 1) {
						out.println(dayString(Integer.parseInt(res.getString(i))));
					}
					else {
						out.println(hourString(Integer.parseInt(res.getString(i))));
					}
					
					out.println("</td>");
				}
				out.println("</tr>");
					
			}
			out.println("</table><br />");
			if (!isActive) {
				out.println("<input type=\"submit\" value=\"Remove Session\" />");
				out.println("</form><br />");
			}
			
			return isActive;

			
		}
		finally {
			try {
				dbConn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}

	static public void printCourseSessionsUser(ServletContext context, JspWriter out, String courseName) 
	throws IOException, SQLException, ClassNotFoundException{
		
		DBConnector dbConn = null;
		try {
			dbConn = new DBConnector();
		

			PreparedStatement stmnt = (PreparedStatement) dbConn.getConnection().prepareStatement(
						"SELECT session_day, session_start_time, session_end_time " +
						"FROM course_sessions " +
						"WHERE course_name = ? " +
						"ORDER BY session_day");
			
			stmnt.setString(1, courseName);
			ResultSet res = stmnt.executeQuery();
			
			out.println(
				
					"<table border=\"10px\">"		 +
					"<tr>"							 +
					"<th>Day</th>"			 		 +
					"<th>Start Time</th>"			 +
					"<th>End Time</th>"				 +
					"</tr>");	
			
			int numCol = res.getMetaData().getColumnCount();
			while (res.next()) {
				out.println("<tr>");
				
				for (int i = 1; i <= numCol; i++) {
					out.println("<td>");
					if (i == 1) {
						out.println(dayString(Integer.parseInt(res.getString(i))));
					}
					else {
						out.println(hourString(Integer.parseInt(res.getString(i))));
					}
					
					out.println("</td>");
				}
				out.println("</tr>");
					
			}
			out.println("</table>");
		}
		finally {
			try {
				dbConn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	
	
	static public void printCourseDescription(ServletContext context, JspWriter out, String courseName) 
	throws IOException, SQLException, ClassNotFoundException{
		
		DBConnector dbConn = null;
		try {
			dbConn = new DBConnector();
		
	
			PreparedStatement stmnt = (PreparedStatement) dbConn.getConnection().prepareStatement(
						"SELECT description " +
						"FROM courses " +
						"WHERE course_name = ? ");
			
			stmnt.setString(1, courseName);
			ResultSet res = stmnt.executeQuery();
			
			res.next();
			out.println(res.getString("description").replaceAll("\n", "<br />"));
		}
		finally {
			try {
				dbConn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	static public void printUsers(ServletContext context, JspWriter out, String action, boolean admins) 
	throws IOException, SQLException, ClassNotFoundException{
		
		DBConnector dbConn = null;
		try {
			dbConn = new DBConnector();
		
	
			PreparedStatement stmnt = (PreparedStatement) dbConn.getConnection().prepareStatement(
						"SELECT username, first_name, last_name, phone_number " + 
						"FROM users WHERE is_admin = ?");
						
			
			stmnt.setBoolean(1, admins);
			ResultSet res = stmnt.executeQuery();
			boolean isEmpty;
			isEmpty = !res.next(); //will be true if reultset is empty
			res.beforeFirst();
			if (isEmpty) {
				out.println("<h2> No students in the system<h2>");
				return;
			}
			out.println(
					"<form method=\"post\" action=\"" + action +"\">"  +
					"<table border=\"10px\">"		 +
					"<tr>"							 +
					"<th>Select</th>"			 +
					"<th>Username</th>"			 +
					"<th>First Name</th>"				 +
					"<th>Last Name</th>"				 +
					"<th>Phone Number</th>"				 +
					"</tr>");	
			
			int numCol = res.getMetaData().getColumnCount();
			while (res.next()) {
				out.println("<tr> <td> <input type = \"checkbox\" name=\"check" 
						+ "\" value =\"" + res.getString(1) + "\" /> </td>");
				
				for (int i = 1; i <= numCol; i++) {
					out.println("<td>");
					if (!admins && i == 1) { // username col
						out.println("<a href=\"user_courses.jsp?username=" + res.getString(i) + "\">");
					}
					
					out.println(res.getString(i));
					if (!admins && i == 1) { // name col
						out.println("</a>");
					}
					
					out.println("</td>");
				}
				out.println("</tr>");
					
			}
			out.println("</table><br />");
			out.println("<input type=\"submit\" value=\"Remove Users\" />");
			out.println("</form>");
		}
		finally {
			try {
				dbConn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}
	
	
	
	
}
